package com.docker.drug.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.docker.common.annotation.Log;
import com.docker.common.core.controller.BaseController;
import com.docker.common.core.domain.AjaxResult;
import com.docker.common.enums.BusinessType;
import com.docker.drug.domain.DockerDrug;
import com.docker.drug.service.IDockerDrugService;
import com.docker.common.utils.poi.ExcelUtil;
import com.docker.common.core.page.TableDataInfo;

/**
 * 药品信息Controller
 * 
 * @author lin
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/drug/drug")
public class DockerDrugController extends BaseController
{
    @Autowired
    private IDockerDrugService dockerDrugService;
//    通过药品编码字符串数组查询药品信息
    @PreAuthorize("@ss.hasPermi('drug:drug:DrugByCodes')")
    @GetMapping("/DrugByCodes")
    public AjaxResult selectDockerDrugByCodes(String drugCode){
        DockerDrug dockerDrugs = dockerDrugService.selectDockerDrugByCodes(drugCode);
        if(dockerDrugs==null){
            return error("药品信息不存在");
        }else {
            return success(dockerDrugs);
        }
    }


//   返回最大药品编码+1
    @PreAuthorize("@ss.hasPermi('drug:drug:selectMaxCode')")
    @GetMapping("/selectMaxCode")
    public AjaxResult selectMaxCode(){
        String maxCode = dockerDrugService.selectMaxCode();
//        将maxCode转为long类型+1
        long maxCodeLong = Long.parseLong(maxCode);
        System.out.println(maxCode+1);
        return success(maxCodeLong+1);
    }

    /**
     * 查询药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('drug:drug:list')")
    @GetMapping("/list")
    public TableDataInfo list(DockerDrug dockerDrug)
    {
        startPage();
        List<DockerDrug> list = dockerDrugService.selectDockerDrugList(dockerDrug);
        return getDataTable(list);
    }

    /**
     * 导出药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('drug:drug:export')")
    @Log(title = "药品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DockerDrug dockerDrug)
    {
        List<DockerDrug> list = dockerDrugService.selectDockerDrugList(dockerDrug);
        ExcelUtil<DockerDrug> util = new ExcelUtil<DockerDrug>(DockerDrug.class);
        util.exportExcel(response, list, "药品信息数据");
    }

    /**
     * 获取药品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('drug:drug:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dockerDrugService.selectDockerDrugById(id));
    }

    /**
     * 新增药品信息
     */
    @PreAuthorize("@ss.hasPermi('drug:drug:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DockerDrug dockerDrug)
    {
        return toAjax(dockerDrugService.insertDockerDrug(dockerDrug));
    }

    /**
     * 修改药品信息
     */
    @PreAuthorize("@ss.hasPermi('drug:drug:edit')")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DockerDrug dockerDrug)
    {
        return toAjax(dockerDrugService.updateDockerDrug(dockerDrug));
    }

    /**
     * 删除药品信息
     */
    @PreAuthorize("@ss.hasPermi('drug:drug:remove')")
    @Log(title = "药品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dockerDrugService.deleteDockerDrugByIds(ids));
    }
}
